<?php

class mailTaskActions extends automailTaskActions
{
	/**
	 * send first mails from list with limit (first vith high priority)
	 */
    public function executeSendMailTasks()
    {
        MailTask::sendMailTasks();
        return $this->redirect('mailtask/list');
    }
    /**
     * 
     * Create mailtasks for selected users
     */
    public function executeBulktasks()
    {
    	$webRequest = new sfWebRequest();
      	$host = $webRequest->getHost();
    	if( $this->getRequest()->getMethod() == sfRequest::POST )
    	{
    		$subject = $host;
    		if($this->getRequestParameter('subject') && ($this->getRequestParameter('subject')!=''))
    		{
    			$subject = $this->getRequestParameter('subject');
    		}
    		$priority = MailTask::PRIORITY_NORMAL;
    	    if($this->getRequestParameter('priority') && ($this->getRequestParameter('priority')!=''))
            {
                $priority = $this->getRequestParameter('subject');
            }
    		$c = new Criteria();
    		if( $this->getRequestParameter('users') )
    		{
    			foreach($this->getRequestParameter('users') as $role )
    			{
    			 $c->add(UserPeer::ROLES, UserPeer::ROLES . ' REGEXP "[[:<:]]' . $role . '[[:>:]]"', Criteria::CUSTOM);
    			}
    		}
            $users = UserPeer::doSelect($c);
    		/* @var $user User */
            if(($this->getRequestParameter('body'))&&($this->getRequestParameter('body') != ''))
            {
	    		$body = $this->getRequestParameter('body');
	    		foreach( $users as $user )
	    		{
	    			$bodyNew = preg_replace('/[%]([A-Za-z0-9_]+)[%]/', $user->getNick(), $body);
	    			$mailto = $user->getEmail();
	    			$title = $subject;
	    			$sender = $host;
	    			$mailfrom = sfConfig::get('app_webmaster_mail');
	    			MailTask::createNew(MyConstants::UNDEFINED_ID, $priority, $subject, $mailto, $title, $bodyNew, $sender, $mailfrom);
	    		}
            }
            $this->setFlash('status', MyConstants::STATUS_APPROVED);
    	}
    	
    }

}
